﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.ComponentModel;

namespace PatientInformationExtract
{
    class CustomTreeViewItem:TreeViewItem
    {
        public CustomTreeViewItem()
        {
            this.isChecked = false;
        }
        public Boolean? IsChecked
        {
            get { return this.isChecked; }
            set
            {
                if (this.isChecked != value)
                {
                    this.isChecked = value;
                    NotifyPropertyChanged("IsChecked");
                    if (this.isChecked == true) // 如果节点被选中
                    {
                        if (this.Items.Count != 0)
                            foreach (CustomTreeViewItem it in this.Items)
                                it.IsChecked = true;
                        if (this.Parent is CustomTreeViewItem)
                        {
                            Boolean bExistUncheckedChildren = false;
                            foreach (CustomTreeViewItem dt in (this.Parent as CustomTreeViewItem).Items)
                            {
                                if (dt.IsChecked != true)
                                {
                                    bExistUncheckedChildren = true;
                                    break;
                                }
                            }
                            if (bExistUncheckedChildren)
                                (this.Parent as CustomTreeViewItem).IsChecked = null;
                            else
                                (this.Parent as CustomTreeViewItem).IsChecked = true;
                        }
                    }
                    else if (this.isChecked == false)   // 如果节点未选中
                    {
                        if (this.Items.Count != 0)
                            foreach (CustomTreeViewItem dt in this.Items)
                                dt.IsChecked = false;
                        if (this.Parent is CustomTreeViewItem)
                        {
                            Boolean bExistCheckedChildren = false;
                            foreach (CustomTreeViewItem dt in (this.Parent as CustomTreeViewItem).Items)
                            {
                                if (dt.IsChecked != false)
                                {
                                    bExistCheckedChildren = true;
                                    break;
                                }
                            }
                            if (bExistCheckedChildren)
                                (this.Parent as CustomTreeViewItem).IsChecked = null;
                            else
                                (this.Parent as CustomTreeViewItem).IsChecked = false;
                        }
                    }
                    else
                    {
                        if (this.Parent is CustomTreeViewItem)
                            (this.Parent as CustomTreeViewItem).IsChecked = null;
                    }
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        private Boolean? isChecked;
    }
}
